<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="计划编码" prop="planCode">
        <el-input
            v-model="queryParams.planCode"
            placeholder="请输入计划编码"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
            v-model="queryParams.productCode"
            placeholder="请输入产品编号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM编号" prop="bomCode">
        <el-input
            v-model="queryParams.bomCode"
            placeholder="请输入BOM编号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM版本" prop="bomVersion">
        <el-input
            v-model="queryParams.bomVersion"
            placeholder="请输入BOM版本"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['pro:record:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column label="计划编码" align="center" prop="planCode">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleInfoView(scope.row)">{{ scope.row.planCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="productCode"/>
      <el-table-column label="产品名称" align="center" prop="productName"/>
      <el-table-column label="BOM编号" align="center" prop="bomCode"/>
      <el-table-column label="BOM版本" align="center" prop="bomName"/>
      <el-table-column label="旧BOM编号" align="center" prop="oldBomCode"/>
      <el-table-column label="旧BOM版本" align="center" prop="oldBomName"/>
      <el-table-column label="变更时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改生产计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="optType == 'view'">
        <el-row>
          <el-col :span="12">
            <el-form-item label="计划编号" prop="planCode">
              <el-input v-model="form.planCode" placeholder="请输入计划编号">
                <el-button slot="append" icon="el-icon-search" @click="handlePlanSelect"></el-button>
              </el-input>
            </el-form-item>
            <PlanningSelect ref="planSelect" @onSelected="onPlanSelected" :status="planStatus"></PlanningSelect>
          </el-col>

          <el-col :span="12">
            <el-form-item label="计划名称" prop="planName">
              <el-input v-model="form.planName" placeholder="请输入计划名称" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="来源类型" prop="orderSource">
              <el-select v-model="form.orderSource" placeholder="请选择来源类型" clearable :disabled="true">
                <el-option
                    v-for="dict in dict.type.mes_workorder_sourcetype"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.orderSource == 'ORDER'">
            <el-form-item label="订单编号" prop="sourceCode">
              <el-input v-model="form.sourceCode" placeholder="请输入订单编号" label-width="100px" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="productSpc">
              <el-input v-model="form.productSpc" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="计划类型" prop="workorderType">
              <el-select v-model="form.workorderType" placeholder="请选择计划类型" :disabled="true">
                <el-option
                    v-for="dict in dict.type.mes_workorder_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划数量" prop="quantity">
              <el-input-number :min="1" v-model="form.quantity" placeholder="请输入计划数量" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs type="border-card" v-if="form.planId != null">
        <el-tab-pane label="产品制程">
          <RecordBom ref="bomlist" :planning="form" :optType="optType"></RecordBom>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType != 'view'">确 认</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRecord, getRecord, listRecord, updateRecord } from "@/api/mes/pro/record";
import PlanningSelect from "@/components/planSelect/single.vue";
import RecordBom from '@/views/mes/pro/record/bom/bom.vue';

export default {
  name: "Record",
  components: {PlanningSelect, RecordBom},
  dicts: ['mes_workorder_sourcetype', 'pro_planning_status', 'mes_item_product', 'mes_workorder_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      optType: undefined,
      planStatus: 'YIFENJIE',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产计划BOM记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planId: null, planCode: null, productCode: null, productName: null, bomCode: null, bomName: null, bomVersion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planCode: [{required: true, message: '计划编号不能为空', trigger: 'blur'}],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产计划BOM记录列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rId: null,
        planId: null,
        planCode: null,
        productId: null,
        productCode: null,
        productName: null,
        bomId: null,
        bomCode: null,
        bomName: null,
        bomVersion: null,
        bomSpc: null,
        unitOfMeasure: null,
        itemOrProduct: null,
        used: null,
        proportional: null,
        remark: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "修改生产计划BOM";
      this.optType = 'add';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rId = row.rId || this.ids
      getRecord(rId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "生产计划BOM记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs[ "form" ].validate(valid => {
        if (valid) {
          let that = this;
          that.$modal.confirm('是否确认完成BOM变更？').then(function () {
            if (that.form.rId != null) {
              updateRecord(that.form).then(response => {
                that.$modal.msgSuccess("确认成功");
                that.open = false;
                that.getList();
              });
            } else {
              that.form.bomId = that.$refs.bomlist.bomId;
              addRecord(that.form).then(response => {
                that.$modal.msgSuccess("确认成功");
                that.open = false;
                that.getList();
              });
            }
          });
        }
      });
    },

    //选择生产工单
    handlePlanSelect() {
      this.$refs.planSelect.showFlag = true;
    },
    onPlanSelected(row) {
      if (row != undefined && row != null) {
        this.form = row;
        this.form.planId = row.planId;
        this.form.planCode = row.planCode;
        this.form.planName = row.planName;
        this.form.status = 'PREPARE';
      }
    },

    // 查询明细按钮操作
    handleInfoView(row) {
      const rId = row.rId || this.ids;
      getRecord(rId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '查看生产计划';
        this.optType = 'view';
      });
    },
  }
};
</script>
